'
'   XC Engine. A simple engine to drive the Internet Explorer web browser
'   Copyright (C) 2009 Stanislav Hordiyenko
'
'   This program is free software; you can redistribute it and/or modify
'   it under the terms of the GNU General Public License as published by
'   the Free Software Foundation; either version 3 of the License, or
'   (at your option) any later version.
'
'   This program is distributed in the hope that it will be useful,
'   but WITHOUT ANY WARRANTY; without even the implied warranty of
'   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'   GNU General Public License for more details.
'
'   You should have received a copy of the GNU General Public License
'   along with this program; if not, see http://www.gnu.org/licenses
'   or write to the Free Software Foundation,Inc., 51 Franklin Street,
'   Fifth Floor, Boston, MA 02110-1301 USA
'

Class Table
	Private objHtmlTable
	Private strHtmlTableUniqueId
	
	Public Sub Class_Initialize()
		Set objHtmlTable = Nothing
	End Sub
	
	Public Sub Class_Terminate()
		Set objHtmlTable = Nothing
	End Sub
	
	Private Function IsTable(ByVal objHtmlElement)
		IsTable = False
		
		If UCase(objHtmlElement.TagName) = "TABLE" Then
			IsTable = True
		End If
	End Function
	
	Public Sub WrapElement(ByVal objHtmlElement)
		If Not (objHtmlElement Is Nothing) Then
			If IsTable(objHtmlElement) Then
				strHtmlTableUniqueId = objHtmlElement.UniqueId
			
				Set objHtmlTable = objHtmlElement
				
				Exit Sub
			End If
		End If

		Err.Raise 10001, "Table->WrapElement", "The Html Element couldn't be wrapped"
	End Sub
	
	Public Function Exists()
		On Error Resume Next
		
		Exists = False
		
		If Not IsTable(objHtmlTable) Then Exit Function
		
		strHtmlElementTagName = objHtmlTable.TagName
		
		If Err.Number = 0 Then
			Exists = True
			
			Exit Function
		End If
		
		Err.Clear
		
		On Error Goto 0
	End Function
	
	Public Function Element()
		Set Element = Nothing
		
		If Not Exists() Then
			Err.Raise 10002,  "Table->Element", "The Table doesn't exist"
			
			Exit Function
		End If
		
		Set Element = objHtmlTable
	End Function
	
	Public Function GetProperty(ByVal strPropertyName)
		If Exists() Then
			If Internal_GetProperty(objHtmlTable, strPropertyName, varPropertyName) Then
				GetProperty = varPropertyName
			Else
				Err.Raise 10003, "Table->GetProperty", "The Table doesn't support this property"
			End If
		Else
			Err.Raise 10002, "Table->GetProperty", "The Table doesn't exist"
		End If
	End Function
	
	Public Function RowsCount()
		If Not Exists() Then
			Err.Raise 10002, "Table->RowsCount", "The Table doesn't exist"
		Else
			RowsCount = CInt(objHtmlTable.Rows.Length)
		End If
	End Function
	
	Public Function ColumnsCount(ByVal intRowIndex)
		If Not Exists() Then
			Err.Raise 10002, "Table->ColumnsCount", "The Table doesn't exist"
		Else
			If intRowIndex > 0 And intRowIndex <= RowsCount Then	
				ColumnsCount = CInt(objHtmlTable.Rows(intRowIndex-1).Cells.Length)
			Else
				Err.Raise 10007, "Table->CellText", "The row index is out of range"
			End If
		End If
	End Function
	
	Public Function CellText(ByVal intRowIndex, ByVal intColumnIndex)
		If Not Exists() Then
			Err.Raise 10002, "Table->CellText", "The Table doesn't exist"
		Else
			If intRowIndex > 0 And intRowIndex <= RowsCount() Then
				If intColumnIndex > 0 And intColumnIndex <= ColumnsCount(intRowIndex) Then
					CellText = Trim(objHtmlTable.Rows(intRowIndex-1).Cells(intColumnIndex-1).InnerText)
				Else
					Err.Raise 10008, "Table->CellText", "The column index is out of range"
				End If
			Else
				Err.Raise 10007, "Table->CellText", "The row index is out of range"
			End If
		End If
	End Function
	
	Public Function ChildElements(ByVal strElementProperties)
		If Not Exists() Then
			Err.Raise 10002, "Table->ChildElements", "The Table doesn't exist"
		Else
			Set objElementsCollection = New ElementsCollection
		
			Call Internal_FilterElements(objHtmlTable, strElementProperties, objElementsCollection)
			
			Set ChildElements = objElementsCollection
		End If
	End Function
	
	Public Function CellElement(ByVal intRowIndex, ByVal intColumnIndex)
		If Not Exists() Then
			Err.Raise 10002, "Table->CellElement", "The Table doesn't exist"
		Else
			If intRowIndex > 0 And intRowIndex <= RowsCount() Then
				If intColumnIndex > 0 And intColumnIndex <= ColumnsCount(intRowIndex) Then
					Set objGenericElement = New GenericElement
					
					objGenericElement.WrapElement(objHtmlTable.Rows(intRowIndex-1).Cells(intColumnIndex-1))
				
					Set CellElement = objGenericElement
				Else
					Err.Raise 10008, "Table->CellText", "The column index is out of range"
				End If
			Else
				Err.Raise 10007, "Table->CellText", "The row index is out of range"
			End If
		End If
	End Function
End Class